﻿Imports OpenDelib.ServiceDeliberations

Public Class GenerationRecueil
    Inherits OpenDelibPage

    Protected ReadOnly Property CodeDossierRecueil As String
        Get
            Return Tools.GetInstance().Parametre(TypeParametre.CodeDossierCreationRecueil)
        End Get
    End Property
    Protected ReadOnly Property EtatRapportRecueil As String
        Get
            Return Tools.GetInstance().Parametre(TypeParametre.EtatDossierCreationRecueil)
        End Get
    End Property

    Public ReadOnly Property RapportsSelectionnes As List(Of String)
        Get
            Dim liste As New List(Of String)

            For Each row As GridViewRow In gvResultats.Rows
                If CType(row.Cells(0).Controls(0), CheckBox).Checked Then liste.Add(Tools.GetRowDataItem(Of Rapport, List(Of Rapport))(Me.gvResultats, row).CodeDossier)
            Next

            Return liste
        End Get
    End Property


    Protected Overrides Sub OnInit(e As EventArgs)
        MyBase.OnInit(e)

        Dim appPath As String = Request.ApplicationPath
        If appPath = "/" Then
            appPath = ""
        End If

        AddCss(Me, appPath & "/Css/Fiche.css")
        AddJS(Me, "selectioncontrole", appPath & "/Scripts/selection.js")
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Odyssee.WebControles.JQPopupPanel.SetCancelScript(Me.btnAnnuler, False)

        If Not IsPostBack Then
            ' Chargement des formats de sortie
            Dim err As String = Tools.LoadParametersCombo(Me.cmbFormatDeSortie, "formats de sortie", 65, Me.CodeDossierRecueil)
            If err <> "" Then
                Me.NotifyErreur(err)
                Return
            End If

            ' Chargement des rapports
            Using svc As New DeliberationsClient
                Dim params As New ParametresListe
                params.Authentication = New AuthenticationParameters
                params.Authentication.Token = Tools.GetInstance.getToken
                params.Etat = Me.EtatRapportRecueil

                Try
                    Dim result As ResultatListeDossiersDeliberationOfRapportwn7zOZhO = svc.ListeRapports(params)
                    If result.OK Then
                        Me.gvResultats.DataSource = result.Dossiers
                        Me.gvResultats.DataBind()
                    Else
                        NotifyErreur(result.ErrorMessage)
                    End If
                Catch ex As Exception
                    NotifyErreur(ex.Message)
                End Try
            End Using

            Me.Page.SetFocus(Me.cmbFormatDeSortie)
        End If

    End Sub

    Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click
        If Me.cmbFormatDeSortie.SelectedValue = "" Then
            NotifyErreur("Vous devez sélectionner un format de sortie.")
        End If

        If Me.RapportsSelectionnes.Count = 0 Then
            NotifyErreur("Vous devez sélectionner au moins un rapport.")
        End If

        Using svc As New DeliberationsClient
            Dim params As New ParametresGenerationRecueil
            params.Authentication = New AuthenticationParameters
            params.Authentication.Token = Tools.GetInstance.getToken
            params.FormatDeSortie = CInt(Me.cmbFormatDeSortie.SelectedValue)
            params.Rapports = Me.RapportsSelectionnes

            Try
                Dim result As IntegerOperationResult = svc.GenererRecueil(params)
                If result.OK Then
                    ScriptManager.RegisterStartupScript(Me, Me.GetType, "FermeturePopup", Odyssee.WebControles.JQPopupPanel.CreateOKScript(result.Value.ToString), True)
                Else
                    NotifyErreur(result.ErrorMessage)
                End If
            Catch ex As Exception
                NotifyErreur(ex.Message)
            End Try
        End Using
    End Sub

End Class